import { auditDepositApi, getDeptAccountApi, getDepositByCustomerIdApi } from "@/api/financialManagement/receivable/waterVerification";
import FormHook from "@/hook/formHook";
import { message } from "ant-design-vue";
import dayjs from "dayjs";
import { Ref } from "vue";
export default function OperationHook(state: {
  formState: WaterVerification.AuditDepositForm;
  formDataRef: Ref<any>;
  emit: any;
  passData: Ref<WaterVerification.ListRows>;
  loading: Ref<boolean>;
  btnLoading: Ref<boolean>;
  type: Ref<number>;
  companyOpts: Ref<any>;
  accountOpts: Ref<any>;
  getAllCustomer: () => void;
}) {
  const { formState, formDataRef, emit, loading, btnLoading, companyOpts, accountOpts, passData, getAllCustomer } = state;
  const { resetFields, validateZhInputHandle } = FormHook({ formState });
  const submitHandle = (selectNext: boolean) => {
    formDataRef.value.validate().then(() => {
      const params: Parameters<typeof auditDepositApi>[0] = {
        ...formState,
        proceedsTime: dayjs(formState.proceedsTime).format("YYYY-MM-DD HH:mm:ss"),
        approvalState: 2
      };
      if (formState.image && formState.image.length) {
        params.image = formState.image[0];
      } else {
        params.image = "";
      }
      btnLoading.value = true;
      loading.value = true;
      auditDepositApi(params)
        .then((res: any) => {
          const { code } = res;
          if (code === 200) {
            message.success("操作成功!");
            if (selectNext) {
              init(formState.id, true);
            } else {
              emit("submit");
            }
          }
        })
        .finally(() => {
          btnLoading.value = false;
          loading.value = false;
        });
    });
  };
  //保存
  const saveSatusHandle = () => {
    formDataRef.value.validate().then(() => {
      const params: any = {
        ...formState,
        proceedsTime: dayjs(formState.proceedsTime).format("YYYY-MM-DD HH:mm:ss")
      };
      if (formState.image && formState.image.length) {
        params.image = formState.image[0];
      } else {
        params.image = "";
      }
      btnLoading.value = true;
      loading.value = true;
      auditDepositApi(params)
        .then((res: any) => {
          const { code } = res;
          if (code === 200) {
            message.success("操作成功!");
            emit("submit");
          }
        })
        .finally(() => {
          btnLoading.value = false;
          loading.value = false;
        });
    });
  };
  const resetHandle = () => {
    resetFields();
    formDataRef.value.clearValidate();
    emit("update:visible", false);
  };
  const filterOption = (input: string, option: any) => {
    return String(option.label).toLowerCase().indexOf(String(input).toLowerCase()) >= 0;
  };

  const getCompanyAndAccount = (e: string) => {
    getDeptAccountApi(e).then((res: any) => {
      const { data } = res;
      const { deptAccounts } = data[0];
      //获取经营单位列表
      companyOpts.value = [{ label: data[0].deptName, value: data[0].deptId }];
      formState.deptId = data[0].deptId;
      //获取收款账户列表
      accountOpts.value = deptAccounts
        .map((t: any) => {
          return {
            name: t.name,
            id: t.id,
            ...t
          };
        })
        .filter((t: any) => t.status);
    });
  };

  const changeCompany = (e: string) => {
    formState.deptId = "";
    formState.accountId = "";
    getCompanyAndAccount(e);
  };
  //查找收款方式
  const findPaymentMethod = () => {
    const findPaymentMethod = accountOpts.value.find((value: any) => value.id === formState.accountId);
    formState.paymentMethod = findPaymentMethod?.paymentMethod || "";
  };

  const init = async (id: string, selectNext?: boolean) => {
    loading.value = true;
    await getAllCustomer();
    // await getAccountOpts();

    return getDepositByCustomerIdApi({ id, selectNext: selectNext ? selectNext : false })
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          if (data) {
            getCompanyAndAccount(passData.value.customerId);
            Object.assign(formState, data);
            formState.proceedsTime = dayjs(Number(data.proceedsTime)) as any;
            formState.image = data.image ? [data.image] : [];
            // formState.accountId = [data.deptId, data.accountId] as any;
            formState.accountId = data.accountId as string;
            findPaymentMethod();
          } else {
            message.info("已经是最后一条!");
            emit("submit");
          }
        }
      })
      .finally(() => (loading.value = false));
  };
  return {
    submitHandle,
    resetHandle,
    resetFields,
    validateZhInputHandle,
    filterOption,
    saveSatusHandle,
    changeCompany,
    getCompanyAndAccount,
    init,
    findPaymentMethod
  };
}
